#!/bin/bash
function help {
	echo "Required Environment:" >&2
	echo "  REPLICATION_MANAGER_USER" >&2
	echo "  REPLICATION_MANAGER_PASSWORD" >&2
	echo "  REPLICATION_MANAGER_URL" >&2
	echo "  REPLICATION_MANAGER_CLUSTER_NAME" >&2
	echo "  REPLICATION_MANAGER_HOST_NAME" >&2
	echo "  REPLICATION_MANAGER_HOST_PORT" >&2
}
[ -z $REPLICATION_MANAGER_USER ] && help && exit 1
[ -z $REPLICATION_MANAGER_PASSWORD ] && help && exit 1
[ -z $REPLICATION_MANAGER_URL ] && help && exit 1
[ -z $REPLICATION_MANAGER_CLUSTER_NAME ] && help && exit 1
[ -z $REPLICATION_MANAGER_HOST_NAME ] && help && exit 1
[ -z $REPLICATION_MANAGER_HOST_PORT ] && help && exit 1

set -x

GET="wget -q --no-check-certificate -O- --header Content-Type:application/json"
AUTH_DATA="{\"username\": \"$REPLICATION_MANAGER_USER\", \"password\": \"$REPLICATION_MANAGER_PASSWORD\"}"
TOKEN=$($GET --post-data "$AUTH_DATA" --header Accept:text/html $REPLICATION_MANAGER_URL/api/login)
function get {
	$GET --header Accept:application/json --header "Authorization: Bearer $TOKEN" $@
}

function write_cnf(){
	dir_path="$1"
	tmp_file="/tmp/my.cnf.old"
	use_tmp="N"
	if [ -f "$dir_path/my.cnf" ]; then
		cp "$dir_path/my.cnf" "$tmp_file"
		use_tmp="Y"
	fi

	cp -r /bootstrap/etc/mysql/* "$dir_path/"	

	if [ "$use_tmp" == "Y" ] && [ "$REPLICATION_MANAGER_FORCE_CONFIG" != "true" ]; then 
		first_line=$(head -n 1 "$tmp_file")
		line_count=$(wc -l < "$tmp_file")

		# Check if the first line starts with '# Generated by Signal18'
		if [[ $first_line != '# Generated by Signal18'* ]] && (( line_count > 1 )); then
			mv "$dir_path/my.cnf" "$dir_path/my.cnf.new"
			mv "$tmp_file" "$dir_path/my.cnf"
		else
			mv "$tmp_file" "$dir_path/my.cnf.old"
		fi
	fi
}

get $REPLICATION_MANAGER_URL/api/clusters/$REPLICATION_MANAGER_CLUSTER_NAME/servers/$REPLICATION_MANAGER_HOST_NAME/$REPLICATION_MANAGER_HOST_PORT/config > config.tar.gz
if [ $? -eq 0 ]; then
	mkdir /bootstrap
	cat config.tar.gz  | tar xzvf - -C /bootstrap
	rm -rf  /var/lib/mysql
  mkdir -p /var/lib/mysql
	chown -R mysql:mysql  /var/lib/mysql
  chown -R mysql:mysql /bootstrap/data/.system
	cp -rpn /bootstrap/data/.system /var/lib/mysql
	write_cnf /etc/mysql
	rm -rf config.tar.gz
	rm -rf  /bootstrap
fi

CURRENT_VERSION=$(cat version.txt)
if [ $? -ne 0 ]; then
	CURRENT_VERSION="0.0.0"
fi

get $REPLICATION_MANAGER_URL/api/version > mon-version.txt
MONITOR_VERSION=$(cat mon-version.txt)

# If the version is not the same or the file not exists, download the new version
if [ "$MONITOR_VERSION" != "$CURRENT_VERSION" ] || [ ! -f /usr/bin/replication-manager-cli ]; then
	echo $MONITOR_VERSION > mon-version.txt
	
	get $REPLICATION_MANAGER_URL/static/configurator/bin/replication-manager-cli > replication-manager-cli
	if [ $? -eq 0 ]; then
		cp replication-manager-cli /usr/bin/replication-manager-cli
		chmod +x /usr/bin/replication-manager-cli
	fi
else
	# If the file exists, copy it to the init directory
	if [ -f replication-manager-cli ] && [ ! -f /usr/bin/replication-manager-cli ]; then
		cp replication-manager-cli /usr/bin/replication-manager-cli
		chmod +x /usr/bin/replication-manager-cli
	fi
fi

/usr/bin/mysql_install_db --user=mysql --datadir=/var/lib/mysql --force
systemctl start mysql
